| Conditions | 17 |
| Paths | 8640 |
| Total Lines | 418 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 2 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like $(document).ready often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | var g_chosen_datetimes = []; |
||
| 204 | $(document).ready(function () { |
||
| 205 | // enable / disable date picker |
||
| 206 | $('#id_expire_set').click(function(){ |
||
| 207 | $('#id_expire_date').prop("disabled", !this.checked); |
||
| 208 | if (this.checked) { |
||
| 209 | $("#id_expire_date").focus(); |
||
| 210 | } |
||
| 211 | }); |
||
| 212 | |||
| 213 | var anonOptions = document.getElementById('anonOptions'); |
||
| 214 | $('#hideNames').click(function() { |
||
| 215 | hideNames = this.checked; |
||
| 216 | }); |
||
| 217 | |||
| 218 | $('#isAnonymous').click(function() { |
||
| 219 | isAnonymous = this.checked; |
||
| 220 | if (isAnonymous) { |
||
| 221 | anonOptions.style.display = 'inline'; |
||
| 222 | } else { |
||
| 223 | anonOptions.style.display = 'none'; |
||
| 224 | } |
||
| 225 | }); |
||
| 226 | |||
| 227 | var privateRadio = document.getElementById('private'); |
||
| 228 | var hiddenRadio = document.getElementById('hidden'); |
||
| 229 | var publicRadio = document.getElementById('public'); |
||
| 230 | var selectRadio = document.getElementById('select'); |
||
| 231 | if (privateRadio.checked) { |
||
| 232 | access_type = 'registered'; |
||
| 233 | } else if (hiddenRadio.checked) { |
||
| 234 | access_type = 'hidden'; |
||
| 235 | } else if (publicRadio.checked) { |
||
| 236 | access_type = 'public'; |
||
| 237 | } else if (selectRadio.checked) { |
||
| 238 | access_type = 'select'; |
||
| 239 | } |
||
| 240 | |||
| 241 | isAnonymous = document.getElementById('isAnonymous').checked; |
||
| 242 | hideNames = anonOptions.checked; |
||
| 243 | |||
| 244 | var accessValues = document.getElementById('accessValues'); |
||
| 245 | if (accessValues.value.length > 0) { |
||
| 246 | var list = document.getElementById('selected-search-list-id'); |
||
| 247 | var accessValueArr = accessValues.value.split(';'); |
||
| 248 | for (var i=0; i<accessValueArr.length; i++) { |
||
| 249 | var val = accessValueArr[i]; |
||
| 250 | if (val == '') { |
||
| 251 | continue; |
||
| 252 | } |
||
| 253 | var li = document.createElement('li'); |
||
| 254 | li.id = val; |
||
| 255 | li.className = 'cl_item cl_access_item selected'; |
||
| 256 | var index = val.indexOf('group_'); |
||
| 257 | if (index == 0) { |
||
| 258 | g_chosen_groups.push(val); |
||
| 259 | li.className += ' is-group'; |
||
| 260 | li.appendChild(document.createTextNode(val.substring(6) + " (group)")); |
||
| 261 | list.appendChild(li); |
||
| 262 | } else { |
||
| 263 | index = val.indexOf('user_'); |
||
| 264 | if (index == 0) { |
||
| 265 | g_chosen_users.push(val); |
||
| 266 | li.className = 'cl_item cl_access_item selected'; |
||
| 267 | var username = val.substring(5); |
||
| 268 | $.post(OC.generateUrl('/apps/polls/get/displayname'), {username: username}, function(data) { |
||
| 269 | li.appendChild(document.createTextNode(username + " (" + data + ")")); |
||
| 270 | list.appendChild(li); |
||
| 271 | }); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | var chosenDates = document.getElementById('chosenDates').value; |
||
| 278 | var chosen = ''; |
||
| 279 | if (chosenDates.length > 0) { |
||
| 280 | chosen = JSON.parse(chosenDates); |
||
| 281 | } |
||
| 282 | var text = document.getElementById('text'); |
||
| 283 | var event = document.getElementById('event'); |
||
| 284 | if (event.checked) { |
||
| 285 | chosen_type = event.value; |
||
| 286 | if (chosenDates.length > 0) { |
||
| 287 | g_chosen_datetimes = chosen; |
||
| 288 | } |
||
| 289 | for (var i=0; i<chosen.length; i++) { |
||
| 290 | var date = new Date(chosen[i]*1000); |
||
| 291 | var year = date.getFullYear(); |
||
| 292 | var month = date.getMonth(); |
||
| 293 | var day = date.getDate(); |
||
| 294 | var newDate = new Date(year, month, day).getTime(); //save timestamp without time of day |
||
| 295 | month = '0' + (month+1); //month is 0-11, so +1 |
||
| 296 | day = '0' + day; |
||
| 297 | var dateStr = day.substr(-2) + '.' + month.substr(-2) + '.' + year; |
||
| 298 | var hours = date.getHours(); |
||
| 299 | var minutes = date.getMinutes(); |
||
| 300 | var ms = (hours * 60 * 60 * 1000) + (minutes * 60 * 1000); //time of day in milliseconds |
||
| 301 | hours = '0' + hours; |
||
| 302 | minutes = '0' + minutes; |
||
| 303 | var timeStr = hours.substr(-2) + ':' + minutes.substr(-2); |
||
| 304 | addRowToList(newDate/1000, dateStr, ms/1000); |
||
| 305 | addColToList(ms/1000, timeStr, newDate/1000); |
||
| 306 | } |
||
| 307 | } else { |
||
| 308 | chosen_type = text.value; |
||
| 309 | if (chosenDates.length > 0) { |
||
| 310 | g_chosen_texts = chosen; |
||
| 311 | } |
||
| 312 | for (var i=0; i<chosen.length; i++) { |
||
| 313 | insertText(chosen[i], true); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | var expirepicker = jQuery('#id_expire_date').datetimepicker({ |
||
| 318 | inline: false, |
||
| 319 | onSelectDate: function(date) { |
||
| 320 | var year = date.getFullYear(); |
||
| 321 | var month = date.getMonth(); |
||
| 322 | var day = date.getDate(); |
||
| 323 | var newDate = new Date(year, month, day).getTime()/1000; |
||
| 324 | document.getElementById('expireTs').value = newDate; |
||
| 325 | }, |
||
| 326 | timepicker: false, |
||
| 327 | format: 'd.m.Y' |
||
| 328 | }); |
||
| 329 | |||
| 330 | var datepicker = jQuery('#datetimepicker').datetimepicker({ |
||
| 331 | inline:true, |
||
| 332 | step: 15, |
||
| 333 | todayButton: true, |
||
| 334 | onSelectDate: function(date) { |
||
| 335 | var year = date.getFullYear(); |
||
| 336 | var month = date.getMonth(); |
||
| 337 | var day = date.getDate(); |
||
| 338 | var newDate = new Date(year, month, day).getTime(); //save timestamp without time of day |
||
| 339 | month = '0' + (month+1); //month is 0-11, so +1 |
||
| 340 | day = '0' + day; |
||
| 341 | var dateStr = day.substr(-2) + '.' + month.substr(-2) + '.' + year; |
||
| 342 | addRowToList(newDate/1000, dateStr); |
||
| 343 | }, |
||
| 344 | onSelectTime: function(date) { |
||
| 345 | var hours = date.getHours(); |
||
| 346 | var minutes = date.getMinutes(); |
||
| 347 | var ms = (hours * 60 * 60 * 1000) + (minutes * 60 * 1000); //time of day in milliseconds |
||
| 348 | hours = '0' + hours; |
||
| 349 | minutes = '0' + minutes; |
||
| 350 | var timeStr = hours.substr(-2) + ':' + minutes.substr(-2); |
||
| 351 | addColToList(ms/1000, timeStr); |
||
| 352 | } |
||
| 353 | }); |
||
| 354 | |||
| 355 | $(document).on('click', '.date-row', function() { |
||
| 356 | var tr = $(this).parent(); |
||
| 357 | var dateId = parseInt(tr.attr('id')); |
||
| 358 | var index = tr.index(); |
||
| 359 | var cells = tr[0].cells; //convert jQuery object to DOM |
||
| 360 | for (var i=1; i<cells.length; i++) { |
||
| 361 | var cell = cells[i]; |
||
| 362 | var delIndex = g_chosen_datetimes.indexOf(dateId + parseInt(cell.id)); |
||
| 363 | if (delIndex > -1) { |
||
| 364 | g_chosen_datetimes.splice(delIndex, 1); |
||
| 365 | } |
||
| 366 | } |
||
| 367 | var table = document.getElementById('selected-dates-table'); |
||
| 368 | table.deleteRow(index); |
||
| 369 | }); |
||
| 370 | |||
| 371 | $(document).on('click', '.date-col', function() { |
||
| 372 | var cellIndex = $(this).index(); |
||
| 373 | var timeId = parseInt($(this).attr('id')); |
||
| 374 | var table = document.getElementById('selected-dates-table'); |
||
| 375 | var rows = table.rows; |
||
| 376 | rows[0].deleteCell(cellIndex); |
||
| 377 | for (var i=1; i<rows.length; i++) { |
||
| 378 | var row = rows[i]; |
||
| 379 | var delIndex = g_chosen_datetimes.indexOf(parseInt(row.id) + timeId); |
||
| 380 | if (delIndex > -1) { |
||
| 381 | g_chosen_datetimes.splice(delIndex, 1); |
||
| 382 | } |
||
| 383 | row.deleteCell(cellIndex); |
||
| 384 | } |
||
| 385 | }); |
||
| 386 | |||
| 387 | $(document).on('click', '.text-row', function() { |
||
| 388 | var tr = $(this).parent(); |
||
| 389 | var rowIndex = tr.index(); |
||
| 390 | var name = $(this).html(); |
||
| 391 | var delIndex = g_chosen_texts.indexOf(name); |
||
| 392 | if (delIndex > -1) { |
||
| 393 | g_chosen_texts.splice(delIndex, 1); |
||
| 394 | } |
||
| 395 | var table = document.getElementById('selected-texts-table'); |
||
| 396 | table.deleteRow(rowIndex); |
||
| 397 | }); |
||
| 398 | |||
| 399 | $(document).on('click', '.icon-close', function() { |
||
| 400 | selectItem($(this)); |
||
| 401 | }); |
||
| 402 | |||
| 403 | $(document).on('click', '.icon-checkmark', function() { |
||
| 404 | deselectItem($(this)); |
||
| 405 | }); |
||
| 406 | |||
| 407 | $(document).on('click', '#text-submit', function() { |
||
| 408 | var text = document.getElementById('text-title'); |
||
| 409 | if (text.value.length == 0) { |
||
| 410 | alert('Please enter a text!'); |
||
| 411 | return false; |
||
| 412 | } |
||
| 413 | insertText(text.value); |
||
| 414 | text.value = ''; |
||
| 415 | }); |
||
| 416 | |||
| 417 | $(document).on('click', '.cl_item', function() { |
||
| 418 | var index; |
||
| 419 | var list = document.getElementById('selected-search-list-id'); |
||
| 420 | var isGroup = $(this).hasClass('is-group'); |
||
| 421 | if ($(this).hasClass('selected')) { |
||
| 422 | if (isGroup) { |
||
| 423 | index = g_chosen_groups.indexOf(this.id); |
||
| 424 | } |
||
| 425 | else { |
||
| 426 | index = g_chosen_users.indexOf(this.id); |
||
| 427 | } |
||
| 428 | if (index > -1) { |
||
| 429 | if (isGroup) { |
||
| 430 | g_chosen_groups.splice(index, 1); |
||
| 431 | } |
||
| 432 | else { |
||
| 433 | g_chosen_users.splice(index, 1); |
||
| 434 | } |
||
| 435 | $(this).remove(); |
||
| 436 | } |
||
| 437 | } else { |
||
| 438 | if (!isGroup) { |
||
| 439 | var text = this.id.replace('user_', ''); |
||
| 440 | g_chosen_users.push(this.id); |
||
| 441 | } else { |
||
| 442 | g_chosen_groups.push(this.id); |
||
| 443 | } |
||
| 444 | document.getElementById('user-group-search-box').value = ''; |
||
| 445 | var li = document.createElement('li'); |
||
| 446 | li.id = this.id; |
||
| 447 | li.className = 'cl_item cl_access_item selected' + (isGroup ? ' is-group' : ''); |
||
| 448 | if (!isGroup) { |
||
| 449 | $.post(OC.generateUrl('/apps/polls/get/displayname'), {username: text}, function(data) { |
||
| 450 | li.appendChild(document.createTextNode(text + " (" + data + ")")); |
||
| 451 | list.appendChild(li); |
||
| 452 | }); |
||
| 453 | } else { |
||
| 454 | li.appendChild(document.createTextNode($(this).html())); |
||
| 455 | list.appendChild(li); |
||
| 456 | } |
||
| 457 | $(this).remove(); |
||
| 458 | } |
||
| 459 | }); |
||
| 460 | |||
| 461 | $('.toggleable-row').hover( |
||
| 462 | function() { |
||
| 463 | var td = this.insertCell(-1); |
||
| 464 | td.className = 'toggle-all selected-all'; |
||
| 465 | }, function() { |
||
| 466 | $(this).find('td:last-child').remove(); |
||
| 467 | } |
||
| 468 | ); |
||
| 469 | |||
| 470 | $(document).on('click', '.toggle-all', function() { |
||
| 471 | var children; |
||
| 472 | var i; |
||
| 473 | if ($(this).attr('class').indexOf('selected-all') > -1) { |
||
| 474 | children = $(this).parent().children('.icon-checkmark'); |
||
| 475 | for (i=0; i<children.length; i++) { |
||
| 476 | deselectItem($(children[i])); |
||
| 477 | } |
||
| 478 | $(this).removeClass('selected-all'); |
||
| 479 | $(this).addClass('selected-none'); |
||
| 480 | } else { |
||
| 481 | children = $(this).parent().children('.icon-close'); |
||
| 482 | for (i=0; i<children.length; i++) { |
||
| 483 | selectItem($(children[i])); |
||
| 484 | } |
||
| 485 | $(this).removeClass('selected-none'); |
||
| 486 | $(this).addClass('selected-all'); |
||
| 487 | } |
||
| 488 | }); |
||
| 489 | |||
| 490 | $('input[type=radio][name=pollType]').change(function() { |
||
| 491 | if (this.value == 'event') { |
||
| 492 | chosen_type = 'event'; |
||
| 493 | document.getElementById('text-select-container').style.display = 'none'; |
||
| 494 | document.getElementById('date-select-container').style.display = 'inline'; |
||
| 495 | } else { |
||
| 496 | chosen_type = 'text'; |
||
| 497 | document.getElementById('text-select-container').style.display = 'inline'; |
||
| 498 | document.getElementById('date-select-container').style.display = 'none'; |
||
| 499 | } |
||
| 500 | }); |
||
| 501 | |||
| 502 | $('input[type=radio][name=accessType]').click(function() { |
||
| 503 | access_type = this.value; |
||
| 504 | if (access_type == 'select') { |
||
| 505 | $("#access_rights").show(); |
||
| 506 | $("#selected_access").show(); |
||
| 507 | } else { |
||
| 508 | $("#access_rights").hide(); |
||
| 509 | $("#selected_access").hide(); |
||
| 510 | } |
||
| 511 | }); |
||
| 512 | |||
| 513 | $('input[type=checkbox][name=check_expire]').change(function() { |
||
| 514 | if (!$(this).is(':checked')) { |
||
| 515 | document.getElementById('expireTs').value = ''; |
||
| 516 | } |
||
| 517 | }); |
||
| 518 | |||
| 519 | $('#user-group-search-box').on('input', debounce(function() { |
||
| 520 | var ul = document.getElementById('live-search-list-id'); |
||
| 521 | while(ul.firstChild) { |
||
| 522 | ul.removeChild(ul.firstChild); |
||
| 523 | } |
||
| 524 | var val = $(this).val(); |
||
| 525 | if (val.length < 3) { |
||
| 526 | return; |
||
| 527 | } |
||
| 528 | var formData = { |
||
| 529 | searchTerm: val, |
||
| 530 | groups: JSON.stringify(g_chosen_groups), |
||
| 531 | users: JSON.stringify(g_chosen_users); |
||
| 532 | } |
||
| 533 | $.post(OC.generateUrl('/apps/polls/search'), formData, function(data) { |
||
| 534 | for (var i=0; i<data.length; i++) { |
||
| 535 | var ug = data[i]; |
||
| 536 | var li = document.createElement('li'); |
||
| 537 | li.className = 'cl_item cl_access_item'; |
||
| 538 | if (ug.isGroup) { |
||
| 539 | li.id = 'group_' + ug.gid; |
||
| 540 | li.className += ' is-group'; |
||
| 541 | li.appendChild(document.createTextNode(ug.gid + " (group)")); |
||
| 542 | ul.appendChild(li); |
||
| 543 | } else { |
||
| 544 | li.id = 'user_' + ug.uid; |
||
| 545 | li.appendChild(document.createTextNode(ug.uid + " (" + ug.displayName + ")")); |
||
| 546 | var span = document.createElement('span'); |
||
| 547 | span.id = 'sec_name'; |
||
| 548 | span.appendChild(document.createTextNode(ug.uid)); |
||
| 549 | li.appendChild(span); |
||
| 550 | ul.appendChild(li); |
||
| 551 | } |
||
| 552 | } |
||
| 553 | }); |
||
| 554 | }, 250)); |
||
| 555 | |||
| 556 | $('.live-search-list-user li').each(function(){ |
||
| 557 | $(this).attr('data-search-term', $(this).text().toLowerCase()); |
||
| 558 | }); |
||
| 559 | |||
| 560 | $('.live-search-box-user').on('keyup', function(){ |
||
| 561 | var searchTerm = $(this).val().toLowerCase(); |
||
| 562 | $('.live-search-list-user li').each(function(){ |
||
| 563 | if ($(this).filter('[data-search-term *= ' + searchTerm + ']').length > 0 || searchTerm.length < 1) { |
||
| 564 | $(this).show(); |
||
| 565 | } else { |
||
| 566 | $(this).hide(); |
||
| 567 | } |
||
| 568 | }); |
||
| 569 | }); |
||
| 570 | |||
| 571 | $('.live-search-list-group li').each(function(){ |
||
| 572 | $(this).attr('data-search-term', $(this).text().toLowerCase()); |
||
| 573 | }); |
||
| 574 | |||
| 575 | $('.live-search-box-group').on('keyup', function(){ |
||
| 576 | var searchTerm = $(this).val().toLowerCase(); |
||
| 577 | $('.live-search-list-group li').each(function(){ |
||
| 578 | if ($(this).filter('[data-search-term *= ' + searchTerm + ']').length > 0 || searchTerm.length < 1) { |
||
| 579 | $(this).show(); |
||
| 580 | } else { |
||
| 581 | $(this).hide(); |
||
| 582 | } |
||
| 583 | }); |
||
| 584 | }); |
||
| 585 | |||
| 586 | var form = document.finish_poll; |
||
| 587 | var submit_finish_poll = document.getElementById('submit_finish_poll'); |
||
| 588 | if (submit_finish_poll != null) { |
||
| 589 | submit_finish_poll.onclick = function() { |
||
| 590 | if (g_chosen_datetimes.length === 0 && g_chosen_texts.length == 0) { |
||
| 591 | alert(t('polls', 'Nothing selected!\nClick on cells to turn them green.')); |
||
| 592 | return false; |
||
| 593 | } |
||
| 594 | if (chosen_type == 'event') { |
||
| 595 | form.elements.chosenDates.value = JSON.stringify(g_chosen_datetimes); |
||
| 596 | } |
||
| 597 | else { |
||
| 598 | form.elements['chosenDates'].value = JSON.stringify(g_chosen_texts); |
||
| 599 | } |
||
| 600 | var title = document.getElementById('pollTitle'); |
||
| 601 | if (title == null || title.value.length == 0) { |
||
| 602 | alert(t('polls', 'You must enter at least a title for the new poll.')); |
||
| 603 | return false; |
||
| 604 | } |
||
| 605 | |||
| 606 | if (access_type == 'select') { |
||
| 607 | if (g_chosen_groups.length == 0 && g_chosen_users == 0) { |
||
| 608 | alert(t('polls', 'Please select at least one user or group!')); |
||
| 609 | return false; |
||
| 610 | } |
||
| 611 | form.elements.accessValues.value = JSON.stringify({ |
||
| 612 | groups: g_chosen_groups, |
||
| 613 | users: g_chosen_users |
||
| 614 | }); |
||
| 615 | } |
||
| 616 | form.elements.isAnonymous.value = isAnonymous; |
||
| 617 | form.elements.hideNames.value = hideNames; |
||
| 618 | form.submit(); |
||
| 619 | }; |
||
| 620 | } |
||
| 621 | }); |
||
| 622 | |||
| 623 |
Strict mode is a way to opt-in to a restricted variant of JavaScript. It eliminates some common pitfalls by being less lenient and raising more errors.
Besides, it is also used to fix certain mistakes which made it difficult for JavaScript runtimes to perform certain optimizations.
We generally recommend to only enable strict mode on the function scope and not in the global scope as that might break third-party scripts on your website.